package install.ren.exercise.test.netty;

import io.netty.buffer.ByteBuf;

import java.io.Serializable;
import java.nio.ByteBuffer;

/**
 * 封装 RPC 请求
 *
 * @author wanglin/netboy
 * @since 1.0.0
 */
public abstract class Message implements Serializable {
    private int length;
    private int cmdId;
    private int seqId;
    private int seqId1 = 0;
    private int seqId2 = 0;

    abstract public byte[] toBytes();

    abstract public void toMessage(ByteBuf byteBuf);

    public int getCmdId() {
        return cmdId;
    }

    public void setCmdId(int cmdId) {
        this.cmdId = cmdId;
    }

    public int getSeqId() {
        return seqId;
    }

    public void setSeqId(int seqId) {
        this.seqId = seqId;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getSeqId1() {
        return seqId1;
    }

    public void setSeqId1(int seqId1) {
        this.seqId1 = seqId1;
    }

    public int getSeqId2() {
        return seqId2;
    }

    public void setSeqId2(int seqId2) {
        this.seqId2 = seqId2;
    }

    protected void getHead(ByteBuf buf) {
        this.setLength(buf.readInt());
        this.setCmdId(buf.readInt());
        this.setSeqId(buf.readInt());
        this.setSeqId1(buf.readInt());
        this.setSeqId2(buf.readInt());
    }

    protected void writeHead(ByteBuffer buf) {
        buf.putInt(this.length);
        buf.putInt(this.cmdId);
        buf.putInt(this.seqId);
        buf.putInt(this.seqId1);
        buf.putInt(this.seqId2);
    }

    public long getMsgId(){
        return Long.valueOf(this.seqId1 + this.seqId2 + "");
    }

    @Override
    public String toString() {
        return "length=" + length +
                ", cmdId=" + cmdId +
                ", seqId=" + seqId + " ";
    }
}
